package com.zhengke.controller.tools;

import com.zhengke.common.util.DateUtil;
import com.zhengke.common.util.FastDfsApiOpr;
import com.zhengke.common.util.LoadProperties;
import com.zhengke.pojo.ReturnAjax;
import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * *******************************
 * 项目名称：tvmall-parent
 * 文件名称：tools
 * 类说明：  工具
 * 创建时间：2020/2/24 13:17
 *
 * @author 王鑫
 * @version 1.0
 * *******************************
 */
@Controller
@RequestMapping("tools")
public class ToolsController {

    /**
     * 跳转到图片裁剪页面
     * @return
     */
    @RequestMapping("editPicsPage.do")
    public String editPics(){
        return "tools/editPics";
    }

    @ResponseBody
    @RequestMapping("editPic.do")
    public Object editPic(@RequestParam CommonsMultipartFile file) {

        try {
            String imageFileName = file.getOriginalFilename();
            String extName = imageFileName.substring(imageFileName.lastIndexOf(".") + 1);

            if (file != null && !file.isEmpty()) {
                String path = LoadProperties.findValue("imgPath")+ "/"+ DateUtil.getSystemDateTimeByFormat("yyyyMMddHHmmss")+"." + extName;
                File file1 = new File(path);
                if (!file1.getParentFile().exists()) {
                    file1.getParentFile().mkdirs();
                }
                FileUtils.copyInputStreamToFile(file.getInputStream(), file1);
                //String rtnPath = LoadProperties.findValue("viewUrl")+"?path="+file1.getPath();
                Map<String,Object> data = new HashMap<String, Object>();
                data.put("src", file1.getPath());
                ReturnAjax ra = new ReturnAjax();
                ra.setData(data);
                ra.setMsg("成功");
                ra.setCode("0");
                return ra;
            } else {
                return new ReturnAjax("300", "文件格式不正确，请上传 *.png,*.jpeg,*.gif,*.jpg类型图片");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ReturnAjax("300", "上传失败");
    }
}
